<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
	public $layout = "layouts/main";
	public function __construct(){
		parent::__construct();
		$this->_site_path = config_item( 'site_path' );
		$this->load->model('admin_model');
		$this->load->library('auth');
	}

	/**
	 * 登录
	 * @author 咸洪伟
	 */
	public function index(){
		
		if($this->auth->hasLogin())
		{
			redirect(base_url($this->_site_path.'/site'));
		}
		$this->load->model("config_model");
		$status_arr = array("key"=>"send_sms","value"=>1);
		$is_send_status = $this->config_model->get_values("id","value",$status_arr);
		$this->form_validation->set_rules('user_name', '用户名', 'required|min_length[2]|trim');
		$this->form_validation->set_rules('password', '密码', 'required|trim');
		// $this->form_validation->set_rules('check_sms', '验证码', 'required|trim');
		$data = array();
		$data['send_sms_isshow'] = empty($is_send_status)?0:1;
		if($this->form_validation->run() === FALSE)
		{	
			$data['error_str']=str_replace(array("<p>","</p>"), array("","&nbsp;&nbsp;"), validation_errors());
			$this->load->view_part($this->_site_path.'/site/login',$data);
		}
		else
		{
			$user = $this->admin_model->validate_user( $this->input->post('user_name', TRUE),$this->input->post('password', TRUE));
                        $this->load->library('session');
                        if($data['send_sms_isshow']){
                            $check_sms = $this->session->userdata($user['admin_id']);
                            if($check_sms  && ($check_sms == md5($this->input->post('check_sms')))){
                               $islogin = 1;
                                $this->session->unset_userdata($user['admin_id']);
                            }else{
                                $islogin = 0;
                            }
                        }else{
                            $islogin = 1;
                        }   
			if(!empty($user) && $islogin )
			{
				$login_ip = $this->common->get_client_ip();
				$user['login_ip'] = $login_ip;
				if($this->auth->process_login($user))
				{
					$field = 'login_count';
					$data['last_login_ip'] = $login_ip;
					$data['last_login_time'] = time();
					$attributes['user_name'] = $this->input->post('user_name');
					$attributes['password'] = md5($this->input->post('password'));
					//登陆成功后
					$this->admin_model->login_success_update($field,$data, $attributes);

					redirect(base_url($this->_site_path.'/site'));
				}
				else
				{
					$this->session->set_flashdata('login_error', 'TRUE');
					$data['error_str']='用户名或密码错误';
					$this->load->view_part($this->_site_path.'/site/login',$data);
				}
			}
			else
			{
				sleep(1);				

				$this->session->set_flashdata('login_error', 'TRUE');
				$data['error_str']='用户名或密码无效';
				$this->load->view_part($this->_site_path.'/site/login',$data);
			}
		}
	}

	/**
	 * 登出
	 * @author 咸洪伟
	 */
	public function login_out(){
		$this->auth->process_logout();
	}

	
	//ajax 获取手机验证
	public function ajax_by_system_send_message(){
                $this->load->library('session');
				$user_name = $this->input->post("user_name");
                $use_arr   = $this->admin_model->get_values('user_name','admin_id',array("user_name"=>$user_name));

                if(empty($use_arr)){exit("0");}
                $str_content = "";
                for($i=0;$i<4;$i++){
                    $str_content .= mt_rand(0, 9);
                }
                $session_arr = array($use_arr[$user_name] =>md5($str_content));
                $this->session->set_userdata($session_arr) ;
                $flag = $this->common->by_system_send_message(array($str_content),1,array($use_arr[$user_name]));//根据系统设置 给用户发邮件、短信
                exit("1");
	}
	
}
